package com.odm.communitymanage.ui.mine

import android.graphics.Color
import android.net.Uri
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.*
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.ui.MainActivity
import com.odm.communitymanage.ui.mine.EditData.AvatarFragment
import com.odm.communitymanage.ui.mine.EditData.EditDataFragment
import com.odm.communitymanage.ui.mine.MyActivity.MyActivityFragment
import com.odm.communitymanage.ui.mine.MyComplaint.MyComplaintFragment
import com.odm.communitymanage.ui.mine.MyIdle.MyIdleFragment
import com.odm.communitymanage.ui.mine.MyParkingPlace.MyParkingPlaceFragment
import com.odm.communitymanage.ui.mine.MyPay.MyPayFragment
import com.odm.communitymanage.ui.mine.MySeekHelp.MySeekHelpFragment
import com.odm.communitymanage.ui.mine.MySubmitRepair.MySubmitRepairFragment
import com.odm.communitymanage.util.GlideUtils
import com.odm.communitymanage.widget.AvatarModeUtils
import kotlinx.android.synthetic.main.fragment_mine.*


/**
 * @description: "我的" 页面 路由功能
 * @author: ODM
 * @date: 2020/4/15
 */
class MineFragment : BaseFragment() {

    var funcAdapter : MineFunctionsAdapter ?= null
    /**
     * 功能列表 icon与对应标题
     */
    private val functionItems : MutableList<FunctionItem> =  mutableListOf()
    /**
     * 功能列表 页面对象数组
     */
    private val functionPages : MutableList<BaseFragment> = mutableListOf()

    init {
        functionItems.add(FunctionItem(R.mipmap.ic_seek_help_home , StringUtils.getString(R.string.my_seek_help)))
        functionItems.add(FunctionItem(R.mipmap.ic_idle_home , StringUtils.getString(R.string.my_idle)))
        functionItems.add(FunctionItem(R.mipmap.ic_fix_home , StringUtils.getString(R.string.my_submit_repair)))
        functionItems.add(FunctionItem(R.mipmap.ic_pay_home , StringUtils.getString(R.string.my_pay)))
        functionItems.add(FunctionItem(R.mipmap.ic_community_activity_home , StringUtils.getString(R.string.my_activity)))
        functionItems.add(FunctionItem(R.mipmap.ic_car ,  StringUtils.getString(R.string.my_parking_place)))
        functionItems.add(FunctionItem(R.mipmap.ic_write , StringUtils.getString(R.string.my_complaint)))
        functionItems.add(FunctionItem(R.mipmap.ic_logout , StringUtils.getString(R.string.logout)))

        functionPages.add(MySeekHelpFragment())
        functionPages.add(MyIdleFragment())
        functionPages.add(MySubmitRepairFragment())
        functionPages.add(MyPayFragment())
        functionPages.add(MyActivityFragment())
        functionPages.add(MyParkingPlaceFragment())
        functionPages.add(MyComplaintFragment())
    }

    /**
     * 页面可见时回调
     */
    override fun onSupportVisible() {
        super.onSupportInvisible()
        //更新用户信息
        refreshAvatar()
        refreshUserName()
    }



    override fun initViews() {
        //初始化RecyclerView
        rvFunctions_Mine.layoutManager = LinearLayoutManager(_mActivity)
        funcAdapter = MineFunctionsAdapter(functionItems)
        rvFunctions_Mine.adapter  = funcAdapter
        funcAdapter?.setOnItemClickListener { adapter, view, position ->
            //根据pos的不同执行不同的操作
            if(position + 1 == funcAdapter?.data?.size ){
                //执行退出登录操作
                SnackbarUtils.with(mContentView)
                    .setMessage("退出登录后需重新登录，是否继续？")
                    .setMessageColor(ColorUtils.getColor(R.color.pure_black))
                    .setDuration(SnackbarUtils.LENGTH_LONG)
                    .setBgResource(R.drawable.white)
                    .setAction(StringUtils.getString(R.string.logout), Color.RED) {
                        //清除用户的信息并重启App
                        SPUtils.getInstance(Constants.SP_USER).clear()
                        ActivityUtils.finishAllActivities()
                        ActivityUtils.startActivity(MainActivity::class.java)
                    }
                    .show()
            }else {
                //执行跳转功能页
                startMyFunctionPageByPosition(position)
            }
        }
        //跳转编辑个人资料页
        tvEditMyData_Mine.setOnClickListener {
            start(EditDataFragment())
        }

        //跳转 头像页
        ivAvatar_Mine.setOnClickListener{
            start(AvatarFragment())
        }
    }

    private fun startMyFunctionPageByPosition(pos : Int){
        if(pos < functionPages.size)
            start(functionPages.get(pos))
        else
            ToastUtils.showShort("操作有误，请更换正确的操作")
    }




    /**
     * 更新 用户的头像信息
     */
    private fun refreshAvatar(){
        //从SP中取出头像Uri ,若没有本地URi存储说明要加载默认的带字头像
        val avatarUriString = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_AVATAR_URI , "")
        if( avatarUriString != ""){
            //已有存储
            GlideUtils.getInstance().loadImageUri(
                requireContext(),
                Uri.parse(avatarUriString),
                ivAvatar_Mine
            )
        }else {
            val name = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_NAME,"无")
            setNameAvatar(name , ivAvatar_Mine)
        }
    }

    /**
     * 刷新 用户名
     */
    private fun refreshUserName(){
        val userName = SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_NAME , "未获取到您的信息")
        tvName_Mine?.text = userName
    }

    /**
     * 我的页面 ， 功能列表 适配器
     * @constructor
     */
    class MineFunctionsAdapter (data : MutableList<FunctionItem>)
        : BaseQuickAdapter<FunctionItem, BaseViewHolder>(R.layout.item_mine_function, data) {

        override fun convert(holder: BaseViewHolder, item: FunctionItem) {
            holder.setText(R.id.tv_mine_function  , item.functionItemName)
            holder.setImageResource(R.id.iv_mine_function , item.functionResId)
        }
    }

    /**
     * 我的页面， 功能列表 子项类
     * @property functionResId Int
     * @property functionItemName String
     * @constructor
     */
    data class FunctionItem(
        val functionResId : Int,
        val functionItemName: String
    )

    override fun shouldPopUpCloseDialog(): Boolean {
        return true
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_mine
    }


}